/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 3 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.iteration;

import java.util.Iterator;

/**
 * An abstract base class intended for anonymous override to spontaneously create Iterators
 * over single values derived immediately from other Iterators.
 * This is most useful when you have a collection of a type, but want to
 * iterate over one of the particular values that type contains.
 * @author anorberg
 *
 * @param <In>    Type of the values originally being iterated over.
 * @param <Out>   Type of the values being extracted.
 */
public abstract class IteratorConverter<In, Out> implements Iterator<Out> {

	private Iterator<? extends In> source;
	
	public IteratorConverter(Iterator<? extends In> source){
		this.source = source;
	}
	
	@Override
	public boolean hasNext() {
		return source.hasNext();
	}

	@Override
	public Out next() {
		return convert(source.next());
	}

	@Override
	public void remove() {
		source.remove();
	}
	
	/**
	 * Convert a value of type In to a value of type Out here. The values
	 * being passed to this method, sequentially, represent the values
	 * being taken from the backing iterator. This method is called only during
	 * a call to next().
	 * @param next  The value taken from a call to source.next()
	 * @return      The corresponding Out value derived from this In value. 
	 */
	protected abstract Out convert(In next);

}
